package com.pv.bfc.web;

import java.util.Iterator;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.pv.bfc.dao.UserRepository;
import com.pv.bfc.model.BriefcaseUser;
import com.pv.bfc.utils.SendPassword;
import com.pv.bfc.web.validator.ForgotUsernameValidator;

@SuppressWarnings("deprecation")
public class ForgotUsernameController extends SimpleFormController {
	UserRepository userRepo;
	SendPassword sendPass;

	private static final Log logger = LogFactory
			.getLog(ForgotUsernameController.class);

	public ForgotUsernameController() {
		setCommandClass(BriefcaseUser.class);
		setSuccessView("redirect:passwordSend.jsp");
		setValidator(new ForgotUsernameValidator());
	}

	@SuppressWarnings("unchecked")
	@Override
	protected void doSubmitAction(Object info) throws Exception {
		logger.info("IN ForgotUsernameController - doSubmitAction");

		BriefcaseUser user = (BriefcaseUser) info;

		List lst = userRepo.retreiveUserByEmailId(user.getEmail());

		Iterator itr = lst.iterator();

		while (itr.hasNext()) {
			BriefcaseUser bfcUser = (BriefcaseUser) itr.next();
			sendPass.sendEmail(bfcUser);
		}
		logger.info("OUT ForgotUsernameController - doSubmitAction");
	}

	public void setUserRepo(UserRepository userRepo) {
		this.userRepo = userRepo;
	}

	public void setSendPassword(SendPassword sendPwd) {
		this.sendPass = sendPwd;
	}

}
